package model;

import com.example.drangh.ApiService;
import com.example.drangh.MainActivity;

import bean.HomeBean;
import contract.HomeContract;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HomeModel implements HomeContract.Model {
    private Object ApiService;

    @Override
    public void getData(final HomeContract.CallBack callback) {
        new Retrofit.Builder()
                .baseUrl((String) ApiService)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
               .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiService.class)
                .getData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(HomeBean bean) {
                        HomeContract.CallBack callBack = null;
                        callBack.getSuccess(bean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        MainActivity callBack = null;
                        callBack.getFile(e.toString());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
